package com.ali.juejin.ui.fragment.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.ali.juejin.R
import com.ali.juejin.adapter.HomeFollowAdapter
import com.ali.juejin.base.BaseFragment
import com.ali.juejin.databinding.FragmentHomeFollowBinding
import com.ali.juejin.databinding.ItemHomeFollowBinding
import com.ali.juejin.model.main.HomeModel
import com.blankj.utilcode.util.LogUtils
import kotlinx.android.synthetic.main.fragment_home_follow.*


class FollowFragment: BaseFragment() {
    val model by lazy { ViewModelProvider(activity!!).get(HomeModel::class.java) }
    val adapter by lazy { HomeFollowAdapter<ItemHomeFollowBinding,FollowFragment>(this) }


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val bind = DataBindingUtil.inflate<FragmentHomeFollowBinding>(LayoutInflater.from(context),R.layout.fragment_home_follow,container,false)
        bind.homeToken = sp.getString("token","")
        return bind.root
    }

    override fun initView() {
        follow_recycler.layoutManager = LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)
        follow_recycler.adapter = adapter
        follow_fresh_layout.setOnRefreshListener {
            freshList()
            follow_fresh_layout.finishRefresh(1000)
        }

        follow_fresh_layout.setOnLoadMoreListener {
//            LogUtils.w("加载更多")
            loadMoreList()
            follow_fresh_layout.finishLoadMore(1000)

        }

        model.homeFollowList.observe(this, Observer {
                newValue->
//            LogUtils.w(newValue)
            adapter.fresh(newValue!!)
        })

//        item_login_btn.setOnClickListener {
            //            XPopup.Builder(context)
//                .popupPosition(PopupPosition.Bottom)
//                .hasStatusBarShadow(false)
//                .dismissOnBackPressed(true)
//                .asCustom(LoginPopView(context!!))
//                .show()
//        }
    }


    override fun initData() {
//        follow_fresh_layout.autoRefresh()
    }

    fun freshList(){
        model.getHomeRecommendData()
    }

    fun loadMoreList(){
        model.getHomeRecommendMoreData()
    }

}